package nacos

import (
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"github.com/spf13/viper"
)

func config() ([]constant.ServerConfig, constant.ClientConfig) {
	return []constant.ServerConfig{
			*constant.NewServerConfig(
				viper.GetString("ip_app"),
				viper.GetUint64("port"),
				constant.WithContextPath("/nacos")),
		}, *constant.NewClientConfig(
			constant.WithNamespaceId(viper.GetString("namespace_id")),
			constant.WithTimeoutMs(5000),
			constant.WithNotLoadCacheAtStart(true),
			constant.WithLogDir("./tmp/nacos/log"),
			constant.WithCacheDir("./tmp/nacos/cache"),
			constant.WithLogLevel("debug"),
		)
}
func NewConfigClient() (config_client.IConfigClient, error) {
	sc, cc := config()
	return clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)
}

// 获取nacos配置信息
func GetConfig() (string, error) {
	client, err := NewConfigClient()
	if err != nil {
		return "", err
	}
	return client.GetConfig(vo.ConfigParam{
		DataId: viper.GetString("data_id"),
		Group:  viper.GetString("group")})
}
